package org.softmed.rest.editor.comps;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

import javax.swing.JButton;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.restlet.Client;
import org.restlet.data.Protocol;
import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.UpdateFrame;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.swing.IconManager;

import com.thoughtworks.xstream.XStream;

public abstract class SimpleEditor extends JPanel implements ActionListener {

	JButton minMax = new JButton(IconManager.zoomOut);

	protected URIProvider uriProvider;
	protected String uri;
	protected MigLayout layout;

	protected Client client = new Client(Protocol.HTTP);
	protected XStream streamer = new XStream();

	protected boolean editable = true;
	protected JPanel maxMinPanel;

	public SimpleEditor(URIProvider provider) {
		layout = new MigLayout("left, insets " + EditorUtil.smallestInsets);
		setLayout(layout);
		uriProvider = provider;
		// setup();

		minMax.addActionListener(this);
		minMax.setToolTipText("Show more information");

		EditorUtil.configButton(minMax);
		minMax.setActionCommand("maximize");

		maxMinPanel = new JPanel();

		MigLayout layout = new MigLayout("left,insets 0,gap 0");
		maxMinPanel.setLayout(layout);
		SwingUtil.setSize(maxMinPanel, 20, 20);

		maxMinPanel.add(minMax);

	}

	// public abstract void setup() throws Throwable;

	public abstract void setFullBackground(Color color);

	public abstract void createNew() throws Throwable;

	public abstract void loadEntity(String uri) throws Throwable;

	public abstract void setEntity(Object entity) throws Throwable;

	public abstract Object getEntity();

	public abstract void refresh() throws Throwable;

	public abstract void setEditable(boolean editable);

	public abstract void customMinimize();

	public abstract void customMaximize();

	// public abstract void setCompacted(boolean compacted);

	public String getClassURI() {
		if (uriProvider.getURI().endsWith("/"))
			return uriProvider.getURI() + uri;
		else
			return uriProvider.getURI() + "/" + uri;
	}

	public String getRESTAdminURI() {
		return uriProvider.getURI();
	}

	public String getEntityURI() throws Throwable {
		Object object = getEntity();
		Method getUri = object.getClass().getMethod("getUri", null);
		String uri = (String) getUri.invoke(object, null);
		return uri;
	}

	public void loadEntity(long id) throws Throwable {
		loadEntity(getClassURI() + "/" + id);
	}

	public Client getClient() {
		return client;
	}

	public URIProvider getUriProvider() {
		return uriProvider;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == minMax) {
			if (minMax.getActionCommand().equals("maximize")) {
				maximize();
			} else {
				minimize();
			}
		}

	}

	public void minimize() {
		minMax.setActionCommand("maximize");
		minMax.setIcon(IconManager.zoomIn);
		minMax.setToolTipText("Show more information");
		customMinimize();
	}

	public void maximize() {
		minMax.setActionCommand("minimize");
		minMax.setIcon(IconManager.zoomOut);
		minMax.setToolTipText("Hide information");
		customMaximize();
	}

	protected void showUpdatePanel() {
		EditorUtil.showUpdatePanel();

	}

	protected void hideUpdatePanel() {
		EditorUtil.hideUpdatePanel();

	}
}
